package com.qdxwx.web.controller.user;


import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.account.UserAccountService;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.data.service.user.UserBuyService;
import com.qdxwx.models.account.UserAccount;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.course.CourseStudyHistory;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user/course")
public class UserCoursePlayController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(UserCoursePlayController.class);

	@Autowired
	private CourseService courseService;
	@Autowired
	private CourseNodeService courseNodeService;
	@Autowired
	private UserBuyService userBuyService;
	@Autowired
	private CourseStudyHistoryService courseStudyHistoryService;
	@Autowired
	private UserAccountService userAccountService;

	/**
	 * 进入播放页面
	 */
	@RequestMapping("/play.html")
	public String playVideo(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return "redirect:/auth/login.html";
		}
		long userId = user.getUserId();
		int courseId = TKConvert.toInteger(request.getParameter("courseId"));
		int nodeId = TKConvert.toInteger(request.getParameter("nodeId"));
		CourseNode courseNode = null;
		if (nodeId > 0) {
			courseNode = courseNodeService.get(nodeId);
			if (courseNode != null) {
				if (courseId == 0) {
					courseId = courseNode.getCourseId();
				}
				if (courseId != courseNode.getCourseId()) {
					return toErrorView(request, "课程与所学章节不匹配，请从正确的连接进入！");
				}
			}
		}
		//获取课程
		CourseDTO course = courseService.getDTO(courseId);
		if (course == null) {
			return toErrorView(request, "找不到对记录，可能已被管理员删除，如有疑问，请与本网官服联系！");
		}
		CourseStudyHistory lastStudy = null;
		if (courseNode == null) {
			//TODO:这里要换成直接返回courseNode
			lastStudy = courseStudyHistoryService.getLastByUserId(userId, courseId);
			if (lastStudy != null) {
				courseNode = courseNodeService.get(lastStudy.getNodeId());
			} else {
				List<CourseNode> courseNodeList = courseNodeService.getList(courseId);
				if (courseNodeList.size() > 0) {
					courseNode = courseNodeList.get(0);
				}
			}
			if (courseNode == null) {
				return toErrorView(request, "本课程暂时可播放章节，请等待本网更新后访问！");
			}
			nodeId = courseNode.getNodeId();
		}
		boolean isBuy = userBuyService.checkIsBuy(userId, ModuleType.COURSE.ordinal(), courseNode.getCourseId());
		if (courseNode.getFree() != 1 && course.getUserId() == userId) {
			if (!isBuy) {
				return toErrorView(request, "您未报名【" + course.getCourseName() + "】课程，播放失败！");
			}
		}
		List<CourseNode> courseNodeTree = courseNodeService.getTreeByCourseId(courseId);
		//查询目录
		CourseStudyHistory courseStudyHistory = new CourseStudyHistory();
		courseStudyHistory.setCourseId(courseId);
		courseStudyHistory.setUserId(userId);
		List<CourseStudyHistory> courseStudyHistories = courseStudyHistoryService.getList(courseStudyHistory);
		if (courseStudyHistories != null) {
			//设置是否播放标识
			courseStudyHistoryService.setIsStudy(courseNodeTree, courseStudyHistories);
			//TODO:这里要改成分钟数统计学习进度
			//我的课程学习记录
			int courseHistorySize = courseStudyHistories.size();
			//获取视频节点的总数
			int sonNodeCount = courseNodeService.countVideo(courseId);
			//我的学习进度百分比
			course.setStudyPercent(courseHistorySize * 100 / sonNodeCount);
		}
		mmap.put("isBuy", isBuy);
		mmap.put("course", course);
		mmap.put("courseNode", courseNode);
		mmap.put("playNodeId", nodeId);
		mmap.put("courseNodeTree", courseNodeTree);
		mmap.put("lastTimeStudyHistory", lastStudy);
		mmap.put("playFromType", 2);
		mmap.put("userId", userId);
		mmap.put("user", user);
		UserAccount account = userAccountService.getByUserId(userId);
		mmap.put("userMoney", account.getBalance());
		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("user/uc-course-play");
		}
		return getViewPath("ucenter/uc-course-play");
	}

}